--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 主界面，从scene来，用于组织所有的layer，层级等等。




PlayRankData = class("PlayRankData",function()
    -- 继承
    local ret = {}

    ret.ary_data = nil

    return ret
end)
PlayRankData.__index = PlayRankData
PlayRankData.__instance = nil



function PlayRankData:getInstance()
    if(PlayRankData.__instance==nil)then
        PlayRankData.__instance = PlayRankData:create()
    end
    return PlayRankData.__instance
end

function PlayRankData:create()
    local ret = PlayRankData:new()
    ret:testInit()
    return ret
end




--------------------------------------------------------
-- 初始化
--------------------------------------------------------
function PlayRankData:init(tab)
    -- 
    self:preSave()
    --
    self:readData( tab )
end



-- 测试的初始化，写死的
function PlayRankData:testInit()
    -- 
    self:preSave()
    --
    local dataTab = self:loadSaveData()
    -- 
    self:readData( dataTab )
end



function PlayRankData:readData(dataTab)
    -- 强制赋值
    -- 段位配置 配置
    self.rankLevelScore = { [1] = { score=0     , name="未开窍的智慧" }, 
                            [2] = { score=1000  , name="石器时代的智慧" }, 
                            [3] = { score=2000  , name="青铜III级的智慧" }, 
                            [4] = { score=5000  , name="青铜II级的智慧" }, 
                            [5] = { score=10000  , name="青铜I级的智慧" }, 
                            [6] = { score=20000  , name="觉醒的智慧" }, 
                            [7] = { score=50000  , name="白银III级的智慧" }, 
                            [8] = { score=80000  , name="白银II级的智慧" }, 
                            [9] = { score=100000  , name="白银I级的智慧" }, 
                            [10] = { score=150000  , name="出类拔萃的智慧" }, 
                            [11] = { score=200000  , name="黄金III级的智慧" }, 
                            [12] = { score=500000  , name="黄金II级的智慧" }, 
                            [13] = { score=800000  , name="黄金I级的智慧" }, 
                            [14] = { score=1500000  , name="不可一世的智慧" }, 
                            [15] = { score=3000000  , name="钻石III级的智慧" }, 
                            [16] = { score=5000000  , name="钻石II级的智慧" }, 
                            [17] = { score=10000000  , name="钻石I级的智慧" }, 
                            [18] = { score=15000000  , name="无与伦比的智慧" }, 
                            [19] = { score=20000000  , name="传说级的智慧" }, 
                            }
                            
    if( dataTab.existRank~=nil and dataTab.existRank~=nil ) then
        self.existRank = dataTab.existRank
    else 
        -- mod ： {  [1] = { score=13210, time="2014年12月8日", step=1, name = "无与伦比的智慧" }  }
        self.existRank = {}
    end
end




--------------------------------------------------------
-- 动态计算
--------------------------------------------------------
function PlayRankData:gameEndToRank( score, step )
    local max = getTableCount( self.existRank )
    local time = os.date("%Y-%m-%d %H:%M:%S");
    local name = self:getNameByScore(score)
    cclook(" 获得的段位: %s   --  %s", score, name )
    self.existRank[ max+1 ] = { ["score"]=score, ["time"]=time, ["step"]=step, ["name"] = name }
    self:refreshRank()
    -- 
    self:saveDataLocal()
    -- cclook("排序后的排行榜 : %s", self.existRank )
end


-- 重新排行
function PlayRankData:getNameByScore( score )
    local max = getTableCount( self.rankLevelScore )
    local lev = 1
    local name = ""
    for i=1,max do 
        local tab = self.rankLevelScore[i]
        if( score>=tab.score ) then 
            name = tab.name
        end
    end
    return name
end


-- 重新排行
function PlayRankData:refreshRank(  )
    local max = getTableCount( self.existRank )
    local newRank = {}
    local idx = 1
    
    for i=1, 20 do 
        local deleteId = nil
        local temp = nil
        for id,tab in pairs(self.existRank ) do 
            if( temp==nil or temp.score<tab.score )then 
                temp = tab
                deleteId = id
            end
        end
        if( deleteId~=nil ) then 
            newRank[idx] = temp
            self.existRank[deleteId] = nil
            idx = idx + 1
        end
    end
    self.existRank = newRank
end




function PlayRankData:getRankByScore( score, step )
    for st, tab in pairs(self.existRank) do 
        if (tab.score==score and tab.step==step ) then 
            return st;
        end
    end
    return 21
end




--------------------------------------------------------
-- 初始化 rank plist 
--------------------------------------------------------
local path = cc.FileUtils:getInstance():getWritablePath().."/save/"
local file = path.."rank.plist"

function PlayRankData:preSave()
    cclook(" 文件的保存路径 %s ", path )
    -- 
    local tab = { }
    tab["saveData"] = "1"
    -- 创建目录
    if( cc.FileUtils:getInstance():isDirectoryExist(path)==false )then
        cclog(" 创建文件")
        local bo = cc.FileUtils:getInstance():createDirectory( path )
        cclook(" 创建文件 %s ", bo)
    else
        cclog("文件存在")
    end
    -- 写文件。
    if ( cc.FileUtils:getInstance():isFileExist(file)==false ) then 
        local wrt = cc.FileUtils:getInstance():writeToFile( tab, file )
        cclook("写 plist 文件 %s ",  wrt)
    end
end



function PlayRankData:loadSaveData()
    local tableSee = cc.FileUtils:getInstance():getValueMapFromFile( file )
    cclook(" 解析出来的 PlayRankData %s ", tableSee )
    return tableSee
end



function PlayRankData:saveDataLocal()
    local tab = { }
    if true then 
        tab.existRank = self.existRank
    end
    local wrt = cc.FileUtils:getInstance():writeToFile( tab, file )
    cclook(" 更新 PlayRankData plist 文件 %s ",  wrt)
end


















